namespace NFTRedit
{
    using System;
    using System.IO;

    public class CWDHChunk
    {
        private static readonly uint headerLength = 0x10;
        private static readonly uint MAGIC = 0x43574448;
        private ushort mMaxIndex;
        private ushort mMinIndex;
        private byte[][] mProperties;
        private uint mSize;
        private uint mUnknown;

        public CWDHChunk(BinaryReader reader)
        {
            this.Load(reader);
        }

        private void Load(BinaryReader reader)
        {
            this.ReadHeader(reader);
            this.ReadBody(reader);
        }

        private void ReadBody(BinaryReader reader)
        {
            int num = (this.mMaxIndex - this.mMinIndex) + 1;
            this.mProperties = new byte[num][];
            for (int i = 0; i < num; i++)
            {
                this.mProperties[i] = new byte[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte() };
            }
            for (int j = (int) (3L - ((reader.BaseStream.Position - 1L) % 4L)); j > 0; j--)
            {
                reader.ReadByte();
            }
        }

        private void ReadHeader(BinaryReader reader)
        {
            if (reader.ReadUInt32() != MAGIC)
            {
                throw new FormatException("Chunk is not CWDH format.");
            }
            this.mSize = reader.ReadUInt32();
            this.mMinIndex = reader.ReadUInt16();
            this.mMaxIndex = reader.ReadUInt16();
            this.mUnknown = reader.ReadUInt32();
        }

        public void RecalculateSize()
        {
            int num = (this.mMaxIndex - this.mMinIndex) + 1;
            int num2 = ((int) headerLength) + (3 * num);
            while ((num2 % 4) != 0)
            {
                num2++;
            }
            this.mSize = (uint) num2;
        }

        public void Save(BinaryWriter writer)
        {
            this.WriteHeader(writer);
            this.WriteBody(writer);
        }

        private void WriteBody(BinaryWriter writer)
        {
            int num = (this.mMaxIndex - this.mMinIndex) + 1;
            for (int i = 0; i < num; i++)
            {
                writer.Write(this.mProperties[i][0]);
                writer.Write(this.mProperties[i][1]);
                writer.Write(this.mProperties[i][2]);
            }
            writer.Flush();
            for (int j = (int) (3L - ((writer.BaseStream.Position - 1L) % 4L)); j > 0; j--)
            {
                writer.Write((byte) 0);
            }
        }

        private void WriteHeader(BinaryWriter writer)
        {
            writer.Write(MAGIC);
            writer.Write(this.mSize);
            writer.Write(this.mMinIndex);
            writer.Write(this.mMaxIndex);
            writer.Write(this.mUnknown);
        }

        public int Length
        {
            get
            {
                return (int) this.mSize;
            }
        }

        public byte[][] Properties
        {
            get
            {
                return this.mProperties;
            }
        }
    }
}

